<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Config
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class MY_Config extends CI_Config {
    
    
    
    



    /**
     * Site URL
     *
     * Adds the language and mobile flag into the
     * site_url() function by default.
     *
     * @param string $uri
     * @param bool $add_flags
     * @return string
     */
    public function site_url($uri = '', $add_flags = true, $add_index = true) {

        /* Do we add the flags to the URL */
        if($add_flags !== false) {
            /* Get the existing uri_string */
            if($uri == '/') { $arrUrl = array(); }
            else { $arrUrl = explode('/', $uri); }

            $arrNew = array();

            /* Are we on the mobile version of the site? */
            if($this->item('is_mobile') !== false && !in_array($this->item('mobile_flag'), $arrUrl)) {
                $arrNew[] = $this->item('mobile_flag');
            }

            /* Check to replace the language */
            if(is_bool($add_flags)) {
                $lang = $this->item('language');
                if(!in_array($lang, $arrUrl)) {
                    /* Add in the language */
                    $arrNew[] = $lang;
                }
            } else {
                /* Change the current language code */
                $changed = false;
                if(count($arrUrl) > 0) {
                    $objLang = &load_class('Lang', 'core');
                    foreach($arrUrl as $key => $url) {
                        if($key < 2 && $changed === false) {
                            if($objLang->is_lang_code($url)) {
                                $arrUrl[$key] = $add_flags;
                                $changed = true;
                            }
                        }
                    }
                }

                if($changed === false) {
                    /* Language is not specified in URL */
                    $arrNew[] = $add_flags;
                }
            }

            /* Build the new URI */
            $new = '/' . implode('/', $arrNew) . '/' . implode('/', $arrUrl);

            /* Remove any excess slashes */
            $uri = remove_slash($new);
        }

        /* Return the parent function */
        $uri = parent::site_url($uri);
        
        if($add_index === false) {
            $index = $this->item('index_page');
            if($index != '') {
                $base = parent::site_url();
                $new_base = str_replace($index, '', $base);
                $uri = remove_slash(str_replace($base, $new_base, $uri));
            }
        }
        
        return $uri;
    }
    
    
    
}

?>